package de.breitbeil.jbooking.util;

import org.springframework.beans.support.MutableSortDefinition;
import org.springframework.beans.support.PropertyComparator;
import org.springframework.beans.support.SortDefinition;

public class NullAwarePropertyComparator extends PropertyComparator {

    /**
     * Create a new PropertyComparator for the given SortDefinition.
     * @see MutableSortDefinition
     */
    public NullAwarePropertyComparator(SortDefinition sortDefinition) {
        super(sortDefinition);
    }

    /**
     * Create a PropertyComparator for the given settings.
     * @param property the property to compare
     * @param ignoreCase whether upper and lower case in String values should be ignored
     * @param ascending whether to sort ascending (true) or descending (false)
     */
    public NullAwarePropertyComparator(String property, boolean ignoreCase, boolean ascending) {
        super(property, ignoreCase, ascending);
    }
    
    @Override
    public int compare(Object o1, Object o2) {
        if(o1 == null && o2 == null) {
            return 0;
        }
        if(o1 == null) {
            return -1;
        }
        if(o2 == null) {
            return 1;
        }
        
        return super.compare(o1, o2);
    }
}
